package cn.xiongxf.service.impl;

import cn.xiongxf.service.ITokenService;
import cn.xiongxf.util.AssertUtil;
import cn.xiongxf.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class TokenServiceImpl implements ITokenService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 创建订单的令牌
     */
    @Override
    public String creatToken(String courseIds) {
        // 参数校验
        AssertUtil.isNotNull(courseIds,"非法请求！课程ID不能为空！");
        // 用户ID
        Long loginId = 9L;  // todo
        // token的key：业务键:用户ID:课程ID    如 token : 9 : 2,3,5
        String tokenKey = "token:" + loginId + ":" + courseIds;
        // 创建令牌
        String token = StrUtils.getComplexRandomString(6);
        // 令牌存入Redis
        redisTemplate.opsForValue().set(tokenKey, token, 10, TimeUnit.MINUTES);
        return token;
    }
}
